﻿using System;

namespace CRM.Extensions
{
    public static class DateTimeExtension
    {
        public static int GetTimeStamp(this DateTime dateTime)
        {
            // DateTime _dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
            // //10位的时间戳
            // long timeStamp = Convert.ToInt32(dateTime.Subtract(_dtStart).TotalSeconds);
            // //13位的时间戳
            // //long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds);
            TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 8, 0, 0, 0);
            return Convert.ToInt32(ts.TotalSeconds);
        }
        
        public static DateTime GetDateTime(long timestamp)
        {
            //DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
            //使用上面的方式会显示TimeZone已过时
            DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
            long lTime = long.Parse(timestamp + "0000000");
            TimeSpan timeSpan = new TimeSpan(lTime);
            DateTime targetDt = dtStart.Add(timeSpan).AddHours(8);
            return targetDt;
        }
        
        public static String TimeStampToDate(long timestamp) {
            return GetDateTime(timestamp).ToString("yyyy-MM-dd");
        }
        
        /// <summary>
        /// 获取当前分钟的时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns>yyyy-MM-dd hh:mm:00</returns>
        public static DateTime CurrentMinute(this DateTime dateTime) {
            return new (dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, 0);
        }
    }
}